/*******************************************************************************
 * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
 * @file     transport.c
 * @author   UartTFT Application Team
 * @version  V0.0.1
 * @date     2023-01-01
 * @brief
 ********************************************************************************/

#include "transport.h"
#include "delay.h"
#include "data.h"
#include "bsp.h"
#include "w25qxx.h"
#include "w25n01.h"
#include "tftlcd.h"
#include "module_select.h"
//#include "edma_drv.h"


extern uint8_t lt_screen_dir;

void LT_UI_changeaddr(uint32_t *addr)
{
	if(lt_screen_dir==0)
	{
		*addr += 0; 
	}
	else if(lt_screen_dir==1)
	{
		*addr += second_UI_add;
	}
//	else if(lt_screen_dir==2)
//	{
//		*addr += 30123832;
//	}
//	else if(lt_screen_dir==3)
//	{
//		*addr += 0;
//	}
}

void LT_ReadFlash_UI(uint8_t *pBuffer, uint32_t ReadAddr, uint32_t NumByteToRead)
{
	LT_UI_changeaddr(&ReadAddr);

	if(NumByteToRead > 0)
	{
		if (Flash_Type != 2)
		{
			NorFlash_Read(pBuffer, ReadAddr, NumByteToRead);
		}
		else
		{
			uint16_t page, column, pageremain;

			page = (ReadAddr / 2048);
			column = (ReadAddr % 2048);
			pageremain = 2048 - column;
			// printf("page column pageremain %d   %d %d \r\n",page,column,pageremain);

			if (NumByteToRead <= pageremain)
				pageremain = NumByteToRead;
			for (;;)
			{
				NandFlash_Read(page, column, pBuffer, pageremain);
				if (NumByteToRead == pageremain)
					break;
				else
				{
					pBuffer += pageremain;
					page++;
					column = 0;
					NumByteToRead -= pageremain;
				if (NumByteToRead > 2048)
					pageremain = 2048;
				else
					pageremain = NumByteToRead;
				}
			}
		}
	}
	DelayUS(1); // wav need
}

/********************************************************************************
* Function Name	: LT_ReadFlash
* Description  	: Read data from external FLASH.
* Input        	: - *pBuffer: buffer for read data
				  - ReadAddr: Address to start reading
				  - NumByteToRead: Number of bytes to read
* Output       	: None
* Return       	: None
*********************************************************************************/
void LT_ReadFlash(uint8_t *pBuffer, uint32_t ReadAddr, uint32_t NumByteToRead)
{
	if(NumByteToRead > 0)
	{
		if (Flash_Type != 2)
		{
			NorFlash_Read(pBuffer, ReadAddr, NumByteToRead);
		}
		else
		{
			uint16_t page, column, pageremain;

			page = (ReadAddr / 2048);
			column = (ReadAddr % 2048);
			pageremain = 2048 - column;
			// printf("page column pageremain %d   %d %d \r\n",page,column,pageremain);

			if (NumByteToRead <= pageremain)
				pageremain = NumByteToRead;
			for (;;)
			{
				NandFlash_Read(page, column, pBuffer, pageremain);
				if (NumByteToRead == pageremain)
					break;
				else
				{
					pBuffer += pageremain;
					page++;
					column = 0;
					NumByteToRead -= pageremain;
					if (NumByteToRead > 2048)
						pageremain = 2048;
					else
						pageremain = NumByteToRead;
				}
			}
		}
	}

	DelayUS(1); // wav need
}

/********************************************************************************
* Function Name	: LT_ReadFlash_DMA
* Description  	: Read data from external FLASH.
* Input        	: - *pBuffer: buffer for read data
				  - ReadAddr: Address to start reading
				  - NumByteToRead: Number of bytes to read
* Output       	: None
* Return       	: None
*********************************************************************************/
void LT_ReadFlash_DMA(uint8_t *pBuffer, uint32_t ReadAddr, uint32_t NumByteToRead)
{
	if (NumByteToRead > 0)
	{
		if (Flash_Type != 2)
		{
			uint32_t dma_len = DMA_LEN3;

			if (NumByteToRead <= dma_len)
				dma_len = NumByteToRead;
			for (;;)
			{
				NorFlash_DMA_Read(pBuffer, ReadAddr, dma_len); //DUAL DMA
				NorFlash_DMA_Wait();
				//DelayUS(1); // wav need

				if (NumByteToRead == dma_len)
					break;
				else
				{
					pBuffer += dma_len;
					ReadAddr += dma_len;
					NumByteToRead -= dma_len;
					if (NumByteToRead > DMA_LEN3)
						dma_len = DMA_LEN3;
					else
						dma_len = NumByteToRead;
				}
			}
		}
		else
		{
			uint16_t page, column, pageremain;

			page = (ReadAddr / 2048);
			column = (ReadAddr % 2048);
			pageremain = 2048 - column;

			if (NumByteToRead <= pageremain)
				pageremain = NumByteToRead;
			for (;;)
			{
				NandFlash_DMA_Read(page, column, pBuffer, pageremain);
				NandFlash_DMA_Wait();
				if (NumByteToRead == pageremain)
					break;
				else
				{
					pBuffer += pageremain;
					page++;
					column = 0;

					NumByteToRead -= pageremain;
					if (NumByteToRead > 2048)
						pageremain = 2048;
					else
						pageremain = NumByteToRead;
				}
			}
		}
	}
	DelayUS(1); // wav need
}





//----------------------------------------------------------------------------------
